// pages/address_take/address_take.js
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var area = require('../../utils/area.js');



Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: true,
    whereshow: false, // 地点选择
    areaList: area.areaList, //地址数据
    province: '', //省
    city: '', //市
    county: '', //区
    name: '', //姓名
    tel: "", //电话
    addressDetail:'', //详细地址
    addressId:''//地址id

  },


  // 位置弹出层控制
  upwhere() {
    this.setData({
      whereshow: true
    });
  },

  whereClose() {
    this.setData({
      whereshow: false
    });
  },



  //获取姓名
  bindinputName(event) {
    this.name = event.detail.value;
    this.setData({
      name: this.name
    });
  },

  // 获取电话
  bindinputTel(event) {
    this.tel = event.detail.value;
    this.setData({
      tel: this.tel
    });
  },

  //详情地址 
  bindinputDetail(event) {
    this.addressDetail = event.detail.value;
    this.setData({
      addressDetail: this.addressDetail
    });
  },


  // 获取区域
  getWhere(e) {
    this.province = e.detail.values[0].name
    this.city = e.detail.values[1].name
    this.county = e.detail.values[2].name
    this.setData({
      province: this.province,
      city: this.city,
      county: this.county,
    })
    // 关闭窗口
    this.whereClose()
  },


  // 默认选项
  onChange(event) {
    this.setData({
      checked: event.detail,
    });
  },


  // 提交保存
  saveAddress(){
    let that = this;
   
    if(that.data.name == ""){
      wx.showToast({
        title: '姓名不能为空',
        icon:"none"
      })
      return;
    }


    if(that.data.tel == ""){
      wx.showToast({
        title: '联系电话不能为空',
        icon:"none"
      })
      return;
    }

    if(that.data.city == ""){
      wx.showToast({
        title: '请选择所在区域',
        icon:"none"
      })
      return;
    }

    if(that.data.addressDetail == ""){
      wx.showToast({
        title: '详细地址不能为空',
        icon:"none"
      })
      return;
    }
    util.request(api.Save, {
      id:that.data.addressId,
      name:that.data.name,
      tel:that.data.tel,
      province:that.data.province,
      city:that.data.city,
      county:that.data.county,
      addressDetail:that.data.addressDetail,
      isDefault:that.data.checked
    }, 'post').then(function (res) {
      if (res.errno == 0) {
        wx.showToast({
          title: '保存成功',
        })
        setTimeout(function() {
          //要延时执行的代码
            wx.navigateBack({
              
            })
        }, 1000) //延迟时间
      } else {
        util.myToast(res.errmsg)
      }
    });
  },


  //详情查询
  dratail(addressId){
     let that = this;
     util.request(api.Detail, {
      id:addressId,
    }, 'get').then(function (res) {
      if (res.errno == 0) {
         that.setData({
          id:res.data.id,
          name:res.data.name,
          tel:res.data.tel,
          province:res.data.province,
          city:res.data.city,
          county:res.data.county,
          addressDetail:res.data.addressDetail,
          checked:res.data.isDefault
         })


      } else {
        util.myToast(res.errmsg)
      }
    });

  },





 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var addressId = options.addressId;
    if(addressId =='' ||  addressId ==undefined || addressId == null ){
        return
    }else{
      this.setData({
        addressId: addressId,
      })
      this.dratail(addressId)
      
    }



     
 

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})